package com.qingxin.sys.verifier;

import org.apache.commons.digester.AbstractObjectCreationFactory;
import org.apache.commons.digester.Digester;
import org.apache.commons.digester.Rule;
import org.apache.commons.digester.xmlrules.DigesterRuleParser;
import org.xml.sax.Attributes;

/**
 * 扩展支持XMLImportRule的解析器
 * @author JiWangli
 */
public class XMLRuleSetParser extends DigesterRuleParser {
	
	protected class ImportRuleFactory extends AbstractObjectCreationFactory	{
		protected ImportRuleFactory()	{
		}
		public Object createObject(Attributes attributes) {
			String pathattr = attributes.getValue("path-attr");
			String joinmethod = attributes.getValue("join-method");
			
			Rule importRule = new XMLImportRule(pathattr, joinmethod);
			return importRule;
		}
	}
	
	private class PatternRule extends Rule	{
		private String attrName;
		private String pattern;
		public PatternRule(String attrName) {
			pattern = null;
			this.attrName = attrName;
		}
		public void begin(Attributes attributes){
			pattern = attributes.getValue(attrName);
			if(pattern != null){
				patternStack.push(pattern);
			}
		}
		public void end(){
			if(pattern != null){
				patternStack.pop();
			}
		}
	}
	
	
	@Override
	public void addRuleInstances(Digester digester) {
		super.addRuleInstances(digester);
		
		String ruleClassName = (org.apache.commons.digester.Rule.class).getName();
		
		digester.addFactoryCreate("*/import-rule", new ImportRuleFactory());
		digester.addRule("*/import-rule", new PatternRule("pattern"));
		digester.addSetNext("*/import-rule", "add", ruleClassName);
		
	}
	
}
